package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.UnitService;

/**
 * 单元管理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("/v1.0/units")
public class UnitController {

	@Resource
	private UnitService unitService;

	/**
	 * 【查询】
	 * 
	 * @param servId
	 *            服务编码
	 * @param subServType
	 *            子服务类型
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listUnit(@RequestParam(value = "serv", required = true) String servId,
			@RequestParam(value = "subserv_type", required = false) String subServType, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = unitService.list(servId, subServType);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【慢日志查询】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/slowlog", method = RequestMethod.GET)
	@ResponseBody
	public RespJson slowLog(@PathVariable("unit_id") String unitId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = unitService.slowLog(unitId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【启动】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/start", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson startUnit(@PathVariable("unit_id") String unitId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = unitService.start(unitId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【停止】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/stop", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson stopUnit(@PathVariable("unit_id") String unitId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = unitService.stop(unitId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【备份】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/backup", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson backupUnit(@PathVariable("unit_id") String unitId,
			@RequestBody(required = false) Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			if (paramMap == null) {
				paramMap = new HashMap<>();
			}
			paramMap.put("unitId", unitId);
			respJson = unitService.backup(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【还原】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/restore", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson restoreUnit(@PathVariable("unit_id") String unitId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("unitId", unitId);
			respJson = unitService.restore(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【迁移】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/migrate", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson migrateUnit(@PathVariable("unit_id") String unitId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("unitId", unitId);
			respJson = unitService.migrate(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【重建】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/rebuild", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson rebuildUnit(@PathVariable("unit_id") String unitId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("unitId", unitId);
			respJson = unitService.rebuild(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【隔离】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/isolate", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson isolateUnit(@PathVariable("unit_id") String unitId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = unitService.isolate(unitId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【隔离】
	 * 
	 * @param unitId
	 *            单元编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{unit_id}/recover", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson recoverUnit(@PathVariable("unit_id") String unitId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = unitService.recover(unitId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}
